package com.devin.service.config;

import com.devin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl;

@Configuration
@EnableWebSecurity()
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;
    @Autowired
    private MyAccessDecisionManager myAccessDecisionManager;
    @Autowired
    private MyAccessDeniedHandler myAccessDeniedHandler;
    @Autowired
    private MyAuthenticationFailuerHandler myAuthenticationFailuerHandler;
    @Autowired
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
    @Autowired
    private MyFilterInvocationSecurityMetadataSource myFilterInvocationSecurityMetadataSource;
    @Autowired
    private MyLogoutSuccessHandler myLogoutSuccessHandler;


    //放行资源
    @Override
    public void configure(WebSecurity web) throws Exception {
//        super.configure(web);
        web.ignoring().antMatchers("/jst/**"
                ,"/css/**"
                ,"/img/**"
                ,"/plugins/**"
                ,"/favicon.ico"
                ,"/index.html"    //controller下伪造的index.html文件
                ,"/"
        );
    }

    //xml中的<security:http, 授权配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        super.configure(http);
        http.authorizeRequests()
                .antMatchers("/main","/success","/logoutSuccess","/fail","/error").hasAnyRole("ADMIN","USER")
//                .antMatchers("/users/**").hasRole("USER")
                .anyRequest().authenticated()

//                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
//            @Override
//            public <O extends FilterSecurityInterceptor> O postProcess(O o) {
//                //指定元数据
//                o.setSecurityMetadataSource(myFilterInvocationSecurityMetadataSource);
//                //指定权限决策管理器
//                o.setAccessDecisionManager(myAccessDecisionManager);
//                return o;
//            }
//        })
                .and().formLogin().
                    loginPage("/").   //使用controller下伪装的/index.html
                    loginProcessingUrl("/login").
                    usernameParameter("username").
                    passwordParameter("password").
                    successHandler(myAuthenticationSuccessHandler).
                    failureHandler(myAuthenticationFailuerHandler).permitAll().
                and().logout().
                    logoutUrl("/logout").invalidateHttpSession(true).
                    logoutSuccessHandler(myLogoutSuccessHandler)./*permitAll().*/
                and().exceptionHandling().
                    accessDeniedHandler(myAccessDeniedHandler).
                and().
                    csrf().disable();
    }

    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /*xml中的<security:authentication-manager, 认证配置*/
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        super.configure(auth);
        /*密码加密的service*/
        /*指定加密的方式，BCryptPasswordEncoder*/
        auth.userDetailsService(userService).
                passwordEncoder(bCryptPasswordEncoder());

    }






}
